<?php

#> coreFX.inc.php
#> This code is released under the GNU GPL (http://www.gnu.org/copyleft/gpl.html)

//core functions, regardless of backend

function parse_pubdate($pubdate,$type) { 
	//input : either "9-23-2005 13:43:53" or "2005-09-23 13:43:53"
	$parts = explode(" ",$pubdate); 
	if(strstr($parts[0],"-")) {
		$mydate = explode("-",$parts[0]); 
		$mytime = explode(":",$parts[1]); 
		if(strlen($mydate[0]) > 3) { 
			$Pubdate = mktime($mytime[0],$mytime[1],$mytime[2],$mydate[1],$mydate[2],$mydate[0]);
		} else { 
			$Pubdate = mktime($mytime[0],$mytime[1],$mytime[2],$mydate[0],$mydate[1],$mydate[2]); 
		}
		if($type=="u") { 
			return $Pubdate; 
		} elseif($type=="mysql") { 
			return date("Y-m-d H:i:s",$Pubdate); 
		} else {
			return $pubdate; 
		}
	} else { 
		return $pubdate; 
	}
}

function permalink_title($title) { 
	$wtitle = str_replace(" ","_",strip_tags(strtolower(trim(stripslashes($title))))); 
	$wtitle = str_replace("/","",$wtitle);
	$wtitle = str_replace("\\","",$wtitle);
	$wtitle = str_replace("#","",$wtitle);
	$wtitle = str_replace("!","",$wtitle);
	$wtitle = str_replace("?","",$wtitle);
	$wtitle = str_replace("&","_",$wtitle);	
	$wtitle = str_replace(",","",$wtitle);
	$wtitle = str_replace("__","_",$wtitle); 
	$wtitle = str_replace('"',"",$wtitle); 
	$wtitle = str_replace("'","",$wtitle); 
	$ptitle = $wtitle; 
	return $ptitle;
}


function make_hash($chars) { 
	$validchars = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9");
	$rand_keys = array_rand($validchars,$chars);
	for($k=0;$k<$chars;$k++) { $passr[] = $validchars[$rand_keys[$k]]; } 
	shuffle($passr);
	foreach($passr as $letter) { $hash .= trim($letter); } 
	return $hash; 
}


function anonymize($input,$display) { 
	$possible_ats = array(" at ", " [at] ", "/", "-at-", " @@ ", "@@@", "(at) ", " \@\ ","~at~", "_at_","@at@", "]at[", "&");
	$possible_dots= array(" dot ", " [dot] ", "/", "-dot-", " period ", "&middot;"," (dot) "," \.\ ","...","-","~dot~","[.]","_dot_", " <dot> ", "-", ".skipthispart.", ".nospam.", "]dot[");
	srand((double)microtime()*1000000); 
	$at = rand(0,count($possible_ats)-1);
	$dot = rand(0,count($possible_dots)-1);  
    if(preg_match("/(([\w\.]+))(@)([\w\.]+)\b/i",$input)) {  
        $mask = str_replace(".",$possible_dots[$dot], $input); 
        $mask = str_replace("@",$possible_ats[$at], $mask); 
        $mask = "<a href=\"mailto:".$mask."\">".$display."</a>"; 
		return $mask; 
	} elseif (preg_match("/(http|ftp|https|ftps)(:\/\/)(.*?)([,\.\)!\?\]:;]*(\s|$))/",$input)) { 
		$mask = "<a href=\"".$input."\">".$display."</a>"; 
		return $mask; 
	} else { 
        $mask = $input;  
        $mask = "<a href=\"mailto:".$input."\">$display</a>"; 
		return $mask; 
	} 
} 

function bot_stump($email) { //this one just jumbles an e-mail address 
	$possible_ats = array(" at ", " [at] ", "/", "-at-", " @@ ", "@@@", "(at) ", " \@\ ","~at~", "_at_", "@at@", "]at[", "&");
	$possible_dots= array(" dot ", " [dot] ", "/", "-dot-", " period ", "&middot;"," (dot) "," \.\ ","...","-","~dot~","[.]","_dot_", "-", ".skipthispart.", ".nospam.", "]dot[");
	srand((double)microtime()*1000000); 
	$at = rand(0,count($possible_ats)-1);
	$dot = rand(0,count($possible_dots)-1);  
    if(preg_match("/(([\w\.]+))(@)([\w\.]+)\b/i",$email)) {  
        $mask = str_replace(".",$possible_dots[$dot], $email); 
        $mask = str_replace("@",$possible_ats[$at], $mask); 
		return $mask; 
	} else { 
        $mask = $email;  
		return $mask; 
	} 
}

function itemPreDisplay($text) { 
	global $dateformat,$config; 
	$now = date($dateformat,time()); 
	$text = preg_replace( "/\[google\](.+)\[\/google\]/i", "<a href=\"http://www.google.com/search?q=$1\">$1</a>", $text);
	$text = preg_replace( "/\[yahoo\](.+)\[\/yahoo\]/i", "<a href=\"http://search.yahoo.com/search?p=$1\">$1</a>", $text);
	$text = preg_replace( "/\[wiki\](.+)\[\/wiki\]/i", "<a href=\"http://wikipedia.org/wiki/$1\">$1</a>", $text);
	$text = preg_replace( "/\[dict\](.+)\[\/dict\]/i", "<a href=\"http://dictionary.reference.com/search?q=$1\">$1</a>", $text);	
	$text = preg_replace( "/\[ebay\](.+)\[\/ebay\]/i", "<a href=\"http://search.ebay.com/$1\">$1</a>", $text);
	$text = preg_replace( "/\[a9\](.+)\[\/a9\]/i", "<a href=\"http://a9.com/$1\">$1</a>", $text);
	$text = preg_replace( "/\[imdb\](.+)\[\/imdb\]/i", "<a href=\"http://imdb.com/find?q=$1\">$1</a>", $text);
	$text = preg_replace("/\[code\](.+)\[\/code\]/se","'<pre class=\"code\">'.stripslashes(htmlentities('$1')).'</pre>'",$text); 
	$text = str_replace( "[[time]]", $now, $text);
	$text = str_replace( "[[title]]", addslashes($config[site_title]), $text);
	return $text; 
}

function print_prep($text) { 
	$text = preg_replace( "/\[google\](.+)\[\/google\]/i", "$1 [http://www.google.com/search?q=$1]", $text);
	$text = preg_replace( "/\[yahoo\](.+)\[\/yahoo\]/i", "$1 [http://search.yahoo.com/search?p=$1]", $text);
	$text = preg_replace( "/\[wiki\](.+)\[\/wiki\]/i", "$1 [http://wikipedia.org/wiki/$1]", $text);	
	$text = preg_replace( "/\[dict\](.+)\[\/dict\]/i", "$1 [http://dictionary.reference.com/search?q=$1]", $text);	
	$text = preg_replace( "/\[ebay\](.+)\[\/ebay\]/i", "$1 [http://search.ebay.com/$1]", $text);
	$text = preg_replace( "/\[a9\](.+)\[\/a9\]/i", "$1 [http://a9.com/$1]", $text);	
	//$text = preg_replace('/<a href=\\\"[http|https|ftp]{1}:\/\/(.){4,}\\\">(.+)<\/a>/','$3 [$1://$2]',$text); 
	//$text = preg_replace('/<a href=\"[http|https|ftp]{1}:\/\/(.){4,}\">(.+)<\/a>/','$3 [$1://$2]',$text); 
	$replace = '/(< \s* A \s+ HREF \s* = \s* ([\'"]?) ((?:https?|ftp):\/\/.+?) \2 .*? > .+? <\s*\/A\s*>)/imsx';
	$text	= preg_replace($replace, "$1 [$3]", $text);
	return $text;
}

function makeCommentSafe($text) {
	//$allowed_tags = "<b><i><u><s><em><strong><p><br><br /><sub><sup><code><cite><small>";
	$text = stripslashes($text); 
	$text = preg_replace("/\[code\](.+)\[\/code\]/se","'<pre class=\"code\">'.stripslashes(htmlentities('$1')).'</pre>'",$text); 
	$text = preg_replace('/<script(.?)+>(.+)<\/script>/i','',$text); 
	$text = preg_replace('/<a href=\\\"[http|https|ftp]{1}:\/\/(.){4,}\\\">(.+)<\/a>/','[[$1://$2|$3]]',$text); 
	$text = strip_tags($text,"<pre><b><i><u>"); 
	$text = preg_replace("/\[(i|b|u|s|em|strong|p|br|sub|sup|code|cite|small)\](.+)\[\/(.+)\]/U","<$1>$2</$1>",$text); 
	//the next few lines clean up UBB tags that many users try to use
	$text = preg_replace("/\[b\](.+)\[\/b\]/","<b>$1</b>",$text); 
	$text = preg_replace("/\[i\](.+)\[\/i\]/","<i>$1</i>",$text); 
	$text = preg_replace("/\[u\](.+)\[\/u\]/","<u>$1</u>",$text); 
	//$text = preg_replace("/\[pre\](.+)\[\/pre\]/Use","'<pre>'.str_replace('<br />','',$1).'</pre>'",$text); 
	$text = preg_replace("/[[:space:]]www.([A-Z0-9.)]+)\.[[:space:]]/i", ' <a href="http://www.$1">www.$1</a>. ', $text);
	$text = preg_replace("/[[:space:]]www.([A-Z0-9.]+)[[:space:]]/i", ' <a href="http://www.$1">www.$1</a> ', $text);	
	$text = preg_replace("/[[:space:]](http|ftp)+(s)?:(\\/\\/)((\\w|\\.)+)(\\/)?(\\S+)?/i", " <a href=\"\\0\">$4</a>", $text);	
	$text = preg_replace("/\[url=(http|ftp|https)(:\\/\\/)(.+)\](.+)\[\/url\]/i",'<a href="$1://$3">$4</a>',$text);
	$text = preg_replace( "/\[url=(.+)\](.+)\[\/url\]/", "<a href=\"http://\\1\">\\2</a>", $text);	
	return $text; 
}


function rPass($complexity='2') { //makes a random password
	if($complexity == "1") { 
		$c = array("b","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","y","z");
		$v = array("a","e","i","o","u");
		$rand_c = array_rand($c,4);
		$rand_v = array_rand($v,2);
		$pass = $c[$rand_c[0]].$v[$rand_v[0]].$c[$rand_c[1]]."-".$c[$rand_c[2]].$v[$rand_v[1]].$c[$rand_c[3]];
		return $pass; 		
	} elseif($complexity == "2") { 
		$validchars = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9");
		$len = mt_rand(6,14); 
		$rand_keys = array_rand($validchars, $len);
		for($k=0;$k<$len;$k++) { $passr[] = $validchars[$rand_keys[$k]]; } 
		shuffle($passr);
		foreach($passr as $letter) { $pass .= trim($letter); } 
		return $pass; 		
	} else { 
		$validchars = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","@","&","-","*","^","$","#","!","`",";",":","{","}","|","=","%","(",")");
		$len = mt_rand(10,20); 
		$rand_keys = array_rand($validchars, $len);
		for($k=0;$k<$len;$k++) { $passr[] = $validchars[$rand_keys[$k]]; } 
		shuffle($passr);
		foreach($passr as $letter) { $pass .= trim($letter); } 
		return $pass; 		
	}
}

function cat_to_tag($cat) { 
	$tag = urlencode(strtolower(str_replace(" ","_",$cat))); 
	return $tag; 
}

?>